print('calculate_absorb_power')
import numpy as np

def set_cutplanes(out_file,cutplanes,cut_processing_file):
    from calculate_domain_size import fdtd_domain
    from FDTDGrid import call_cut_dat_by_planes_py,set_threads
    dx = fdtd_domain["dx"]
    dy = fdtd_domain["dy"]
    dz = fdtd_domain["dz"]
    x0 = fdtd_domain["min_x"]
    y0 = fdtd_domain["min_y"]
    z0 = fdtd_domain["min_z"]
    dsize= np.array([dx,dy,dz],dtype=np.float32)
    r0= np.array([ x0,y0,z0 ],dtype=np.float32)
    cut_planes_data = []
    for cutplane in cutplanes:
        cut_planes_data=  cut_planes_data + cutplane['r5'].tolist() + cutplane['iu'].tolist() + cutplane['iv'].tolist() + [float(cutplane['flip'])]
    cut_planes_data= np.array(cut_planes_data,dtype=np.float32)
    print(dsize, r0 ,cut_planes_data)
    set_threads(16)
    call_cut_dat_by_planes_py(out_file, dsize, r0, cut_planes_data, 3, cut_processing_file)
def calculate_absorb_power():
    from define_output_parameters import sampled_electric_fields
    from initialize_fdtd_material_grid import sigma_e_x,density
    from define_problem_space_parameters import number_of_time_steps,time_user
    from initialize_fdtd_parameters_and_arrays import dt
    ##
    number_of_sampled_electric_fields = len(sampled_electric_fields)
    absorb_power=np.zeros(number_of_sampled_electric_fields) #0.5.*ind_E(:)
    SAR = np.zeros(number_of_sampled_electric_fields)
    SurE = np.zeros(number_of_sampled_electric_fields)
    for ind in np.arange(3,number_of_sampled_electric_fields):
        is0 = sampled_electric_fields[ind]["is"]-1
        js = sampled_electric_fields[ind]["js"] -1
        ks = sampled_electric_fields[ind]["ks"] -1
        if sigma_e_x[is0,js,ks]==1e10:
            sigma_e_x[is0,js,ks]=0
        absorb_power[ind] =0.5*sigma_e_x[is0,js,ks]*np.sum(sampled_electric_fields[ind]["sampled_value"][:number_of_time_steps]**2)
        SAR[ind] =0.5*sigma_e_x[is0,js,ks]/density[is0,js,ks]*np.sum(sampled_electric_fields[ind]["sampled_value"][:number_of_time_steps]**2)
        if sigma_e_x[is0,js,ks]==0:
            SurE[ind]= 0;#将空气区域置为0
        else:
            SurE[ind]= sampled_electric_fields[ind]["sampled_value"][time_user]
    #sigma_e_x[44,22,44]  0.7665
    average_power_max = np.max(absorb_power)
    average_power_max = 900e6*dt*average_power_max
    return average_power_max,absorb_power,SAR,SurE  #0.8143
    #5.0350e-05
    #50:0.0060  25:1.6656e-10 15:3.9150e-22
def output_data(data,file,type=1):
    from define_problem_space_parameters import boundary,dx,dy,dz
    #由于absorb power是一维向量，要还原出三维，x_num,y_num,z_num给出三维空间的大小
    import environment_pool as env
    number_of_s = 2
    ind_sample = number_of_s+2
    l_ab = np.size(data)
    if(env.mesh_type_pool==0):
        #TODO:2
        ndenx,ndeny,ndenz = env.brian_pool['n3density'].shape
    else:
        from define_geometry import n3density
        ndenx,ndeny,ndenz = n3density.shape
    data_1D = data[ind_sample-1:l_ab]
    data_3D = data_1D.reshape((ndenx,ndeny,ndenz))
    from fileutils import write_dat,write_bin
    if(type==0):
        np.savez(f"{file}.npz",data=data_1D)
    elif(type==1):
        write_dat(file,[ndenx,ndeny,ndenz],[dx,dy,dz,0,0,0],data_3D)
    elif(type==2):
        write_bin(file,ndenx,ndeny,ndenz,dx,dy,dz,0,0,0,data_3D)